/*
 * @Descripttion:
 * @version:
 * @Author: WANGLONG
 * @Date: 2021-05-25 10:29:48
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-05-25 11:29:23
 */
const fs = require('fs');
const path = require('path');
const modelGenerator = require('./ReactUmi/model.ts');
const viewGenerator = require('./ReactUmi/pages.ts');

const processMain = require('process');

console.log('node:' + processMain.argv[0]);
console.log('脚本:' + processMain.argv[1]);
console.log('参数:' + processMain.argv[2]);
console.log('参数:' + processMain.argv[3]);
// 绝对路径
const absolutePath = path.resolve();

const components = `1`;

console.log();

/*
 * (path, { mode, recursive }, error)
 * recursive 指示是否要创建父目录
 **当 path 是已存在的目录时，调用 fs.mkdir() 仅在 recursive 为 false 时才会导致错误
 */
fs.mkdir(`${absolutePath}/src/models`, { recursive: true }, (error) => {
  if (error) {
    console.log(`model目录创建失败`);
  }
});
fs.mkdir(
  `${absolutePath}/src/pages/${processMain.argv[2]}`,
  { recursive: true },
  (error) => {
    if (error) {
      console.log(`model目录创建失败`);
    }
  },
);

// 生成Model文件
const modelPage = fs.createWriteStream(
  `${absolutePath}/src/models/modules/${processMain.argv[2]}.ts`,
);
modelPage.write(modelGenerator(processMain.argv[2]));
modelPage.end();

// 生成Page文件
const viewPage = fs.createWriteStream(
  `${absolutePath}/src/pages/${processMain.argv[2]}/index.tsx`,
);
viewPage.write(viewGenerator(processMain.argv[2]));
viewPage.end();

// 生成Api文件
// const apiPage = fs.createWriteStream(`${absolutePath}/src/pages/${processMain.argv[2]}.ts`)
// viewPage.write(viewGenerator(processMain.argv[2]))
// viewPage.end();
